package bid.yanhj.cniao5shop.cniao5shop.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import bid.yanhj.cniao5shop.cniao5shop.R;

public class CnToolbar extends Toolbar {

    // 读布局文件
    private LayoutInflater mInflater;

    private View mView;
    private TextView mTextView;
    private EditText mSearchView;
    private ImageButton mRightImageButton;

    public CnToolbar(Context context) {
        this(context, null);
    }

    public CnToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("RestrictedApi")
    public CnToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 写代码

        initView();
        // 设置边距
        setContentInsetsRelative(10, 10);


        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.CnToolbar, defStyleAttr, 0);

            final Drawable navIcon = a.getDrawable(R.styleable.CnToolbar_rightButtonIcon);
            if (navIcon != null) {
//                setNavigationIcon(navIcon);
                setRightBottonIcon(navIcon);
            }

            // 读取isShowSearchView属性
            Boolean isShowSearchView = a.getBoolean(R.styleable.CnToolbar_isShowSearchView, false);
            if (isShowSearchView) {
                showSearchView();
                hideTitleView();
            }

            a.recycle();
        }
    }

    private void initView() {

        if (mView == null) {
            mView = mInflater.inflate(R.layout.toolbar, null);

            mTextView = mView.findViewById(R.id.toolbar_title);
            mSearchView = mView.findViewById(R.id.toolbar_searchview);
            mRightImageButton = mView.findViewById(R.id.toolbar_rightButton);

            // 添加布局
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_HORIZONTAL);

            addView(mView, lp);
        }
    }

    public void setRightBottonIcon(Drawable icon) {

        if (mRightImageButton != null) {
            mRightImageButton.setImageDrawable(icon);
            mRightImageButton.setVisibility(VISIBLE);
        }
    }

    public void setRightButtonOnClickListener(OnClickListener onClickListener) {
        mRightImageButton.setOnClickListener(onClickListener);
    }

    @Override
    public void setTitle(int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTextView != null) {
            showTitleView();
            mTextView.setText(title);
        }
    }

    public void showSearchView() {

        if (mSearchView != null) {
            mTextView.setVisibility(VISIBLE);
        }
    }

    public void hideSearchView() {
        if (mSearchView != null)
            mTextView.setVisibility(GONE);
    }

    public void showTitleView() {
        if (mTextView != null)
            mTextView.setVisibility(VISIBLE);
    }

    public void hideTitleView() {
        if (mTextView != null)
            mTextView.setVisibility(GONE);
    }
}
